using System;
using System.Security.Permissions;
using System.Windows.Forms;
using facebook.Components;
using facebook.Types;

namespace facebook.Forms
{
	internal sealed partial class FacebookExtendedPermission : Form
	{
		private readonly FacebookService _facebookService;
		private readonly Enums.Extended_Permissions _permission;

		private FacebookExtendedPermission()
		{
			InitializeComponent();
		}

		[SecurityPermission(SecurityAction.LinkDemand)]
		internal FacebookExtendedPermission(string loginUrl, Enums.Extended_Permissions permission, FacebookService service)
			: this(new Uri(loginUrl), permission, service)
		{
		}

		[SecurityPermission(SecurityAction.LinkDemand)]
		internal FacebookExtendedPermission(Uri loginUrl, Enums.Extended_Permissions permission, FacebookService service)
			: this()
		{
			_facebookService = service;
			_permission = permission;
			wbFacebookLogin.Navigate(loginUrl);
		}

      
		private void wbFacebookLogin_Navigated(object sender, WebBrowserNavigatedEventArgs e)
		{
			if ((e.Url.PathAndQuery.Contains("authorize.php")==true ) &&
                (e.Url.Fragment == "#"))
			{
			  DialogResult = (_facebookService.users.hasAppPermission(_permission)) ? DialogResult.OK : DialogResult.Cancel;


              MessageBox.Show("test1");
              
               
           
            
            }
            /*else if (e.Url.PathAndQuery.Contains("authorize.php") == true)
            {
                //this.CheckGivenPermision(_permission.ToString());
                MessageBox.Show("test2");
                DialogResult = DialogResult.OK;
            }*/
		}

		private void FacebookAuthentication_FormClosed(object sender, FormClosedEventArgs e)
		{
			if ((DialogResult != DialogResult.OK) &&(DialogResult != DialogResult.None))
			{ 
				DialogResult = DialogResult.Cancel; 
			}
            else if (DialogResult == DialogResult.None)
            {
                DialogResult = DialogResult.None;
            }
		}
	}
}